package org.example.wx.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.common.utils.DateTimeUtil;
import org.example.common.utils.IpUtil;
import org.example.common.utils.ResponseUtil;
import org.example.db.entity.WxUser;
import org.example.db.mapper.WxUserMapper;
import org.example.wx.dto.UserInfo;
import org.example.wx.dto.WxLoginInfoDto;
import org.example.wx.service.WxAuthService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "微信登陆")
@RequestMapping("/wx/auth")
@Slf4j
public class WxAuthController {

    WxAuthService wxAuthService;

    public WxAuthController(WxAuthService wxAuthService) {
        this.wxAuthService = wxAuthService;
    }

    @PostMapping("/login")
    @ApiOperation("微信登陆")
    public Object login(@RequestBody WxLoginInfoDto wxLoginInfoDto, HttpServletRequest request){

        UserInfo userInfo = wxLoginInfoDto.getUserInfo();
        String code = wxLoginInfoDto.getCode();
        if(code == null || userInfo == null){
            return ResponseUtil.badArgument();
        }
        String token = wxAuthService.login(code, userInfo, request);

        return ResponseUtil.ok(token);
    }


}
